package com.example.ordersys.controller;

import com.example.ordersys.config.AppFinal;
import com.example.ordersys.mapper.OrderDetailMapper;
import com.example.ordersys.mapper.OrderInfoMapper;
import com.example.ordersys.model.OrderInfo;
import com.example.ordersys.model.UserInfo;
import com.example.ordersys.tools.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/order")
@RestController
public class OrderInfoController {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    /**
     * 添加订单
     * @return
     */
    @RequestMapping("/add")
    public ResponseBody<Integer> add(String dids, HttpServletRequest request) {
        int data = 0;
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(AppFinal.USERINFO_SESSION_KEY) != null) {
            int uid = ((UserInfo)session.getAttribute(AppFinal.USERINFO_SESSION_KEY)).getId();
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setUid(uid);
            int result = orderInfoMapper.add(orderInfo);
            if(result > 0) {
                data = orderDetailMapper.add(orderInfo.getId(),dids.split(","));
            }
        }
        return new ResponseBody<>(0,"",data);
    }
    /**
     * 个人订单列表
     */
    @RequestMapping("/list")
    public ResponseBody<List<OrderInfo>> list(HttpServletRequest request) {
        List<OrderInfo> data = null;
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(AppFinal.USERINFO_SESSION_KEY) != null) {
            int uid = ((UserInfo)session.getAttribute(AppFinal.USERINFO_SESSION_KEY)).getId();
            data = orderInfoMapper.getList(uid);
        }

        return new ResponseBody<>(0,"",data);
    }

    /**
     * 全部订单
     * @param
     */
    @RequestMapping("/alllist")
    public ResponseBody<List<OrderInfo>> allList(HttpServletRequest request) {
        List<OrderInfo> data = null;
        data = orderInfoMapper.getAllList();
        return new ResponseBody<>(0,"",data);
    }
    /**
     * 修改订单状态
     */
    @RequestMapping("/up")
    public ResponseBody<Integer> up(int id,int status) {
        int data = orderInfoMapper.upStatus(id,status);
        return new ResponseBody<>(0,"",data);
    }
}
